package com.example.file.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.example.file.dao.ImportExcelDao;
import com.example.file.entity.ImportExcelVo;
import com.example.file.mail.Mail;
import com.example.file.mail.SendMessageMail;
import com.example.file.service.FileService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;

@Controller
@RequestMapping("/file")
public class FileController {

    private static final Logger log = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private ImportExcelDao importExcelDao;

    @RequestMapping("/index")
    public String  getUser(Model model) {
        return "file/index";
    }

    @RequestMapping("/pageList")
    @ResponseBody
    public JSONObject getPagedList(Integer limit, Integer page) {
        JSONObject result = new JSONObject();
        if (StringUtils.isEmpty(page.toString()))
        {
            page = 1;
        }
        Map map = new HashMap<>();
        map.put("page",10*(page-1));
        map.put("pageSize",10);
        List<ImportExcelVo> list = importExcelDao.queryAll(map);
        Integer countNum = importExcelDao.countNum();
        result.put("count",countNum);
        result.put("data",list);
        result.put("code",0);
        return new JSONObject(result);
    }

    @PostMapping("/upload")
    @ResponseBody
    public JSONObject excelImport(@RequestParam("file") MultipartFile file) {
        ImportParams importParams = new ImportParams();

        // 需要验证
//        importParams.setNeedVerfiy(true);

        try {
            ExcelImportResult<ImportExcelVo> result = ExcelImportUtil.importExcelMore(file.getInputStream(), ImportExcelVo.class,
                    importParams);

            List<ImportExcelVo> successList = result.getList();
            List<ImportExcelVo> failList = result.getFailList();
            Integer i = 0;

            for (ImportExcelVo importExcelVo : successList) {
                if(importExcelVo==null||importExcelVo.getName()==null){
                    break;
                }
                if(FileService.fillTemplate(importExcelVo,i)) {
                    Mail mail = new Mail();
                    mail.setTo(importExcelVo.getMailbox());
                    mail.setSubject("对账单");
                    mail.setComment("尊敬的合作商：\n" +
                            "您好，\n" +
                            "\n" +
                            "本公司与贵公司在业务往来中友好合作，我公司财务根据出自本公司账簿核实，详情见附件。\n" +
                            ".\n" +
                            "注意：\n" +
                            "1、如与贵公司记录相符，请在附件回执函下端“信息证明无误”处盖章（财务章或公章）。\n" +
                            "2、如有不符，请与我司电话核对（电话：0551-62550058），在“信息不符”处列明不符金额和原因（或附往来账对账表）。\n" +
                            "3、感谢您的大力配合，请在七个工作日内回复，回执函PDF回复至此邮箱： account@fortuneaf.cn 。\n" +
                            "4、若贵单位的财务联系人、邮箱等信息需变更，请及时联系我司。\n" +
                            "\n" +
                            "谢谢合作！");
                    mail.setAffixUrl("D:/sendemailtool/excel/PdfDemoNew" +i+".pdf");
                    Mail.SendMail(mail);
                    i++;
                    importExcelDao.insertImportExcel(importExcelVo);

                }
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        JSONObject result = new JSONObject();
        result.put("msg","发送成功");
        return new JSONObject(result);
    }

}